﻿
#include "getcolorvaluetools.h"

extern bool isMouseButtonPress;
GetColorValueTools::GetColorValueTools(QWidget *parent)
    : QWidget(parent)
{

    initUI();

    // 定时器更新
    QTimer *updateTimer = new QTimer(this);
    updateTimer->callOnTimeout(this,&GetColorValueTools::ShowColorValue);
    updateTimer->start(100);
}

GetColorValueTools::~GetColorValueTools()
{
}

bool GetColorValueTools::eventFilter(QObject *watched, QEvent *ev)
{
    QMouseEvent *mouseEv=static_cast<QMouseEvent*>(ev);
    if(ev->type()==QEvent::MouseButtonPress&& mouseEv->buttons()&Qt::MouseButton::RightButton)    // 事件为 鼠标按下
    {
        qDebug()<<"右键按下"<<isMouseButtonPress;
    }
    return QObject::eventFilter(watched, ev);
}

void GetColorValueTools::initUI()
{
    //窗体居中显示
    QDesktopWidget *desktop=QApplication::desktop();
    int width=desktop->width();
    int height=desktop->height();
    //设置窗口只有最小化和关闭按钮 +窗口将始终显示在其他窗口之上
    setWindowFlags(Qt::WindowMinimizeButtonHint|Qt::WindowCloseButtonHint|Qt::WindowStaysOnTopHint);
    this->move((width-this->width())/2,(height-this->height())/2);
    this->setFixedSize(this->width(),this->height());

    setWindowIcon(QIcon(":/1.ico"));
    setWindowTitle(tr("颜色拾取器"));

    labWeb=new QLabel("Web值：",this);
    labRgb=new QLabel("RGB值：",this);
    labXY=new QLabel("XY值：",this);
    labTip=new QLabel("按下鼠标右键停止/开启",this);
    labColor=new QLabel(this);

    txtWeb=new QLineEdit(this);
    txtRgb=new QLineEdit(this);
    txtXY=new QLineEdit(this);

    // 设置自动调整大小的属性
    labWeb->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labRgb->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labXY->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    labColor->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
    txtWeb->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    txtRgb->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    txtXY->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
    labTip->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);

    QHBoxLayout *hLayoutColor=new QHBoxLayout();
    hLayoutColor->addWidget(labColor);
    QHBoxLayout *hLayoutWeb=new QHBoxLayout();
    hLayoutWeb->addWidget(labWeb,1);
    hLayoutWeb->addWidget(txtWeb,2);
    QHBoxLayout *hLayoutRgb=new QHBoxLayout();
    hLayoutRgb->addWidget(labRgb,1);
    hLayoutRgb->addWidget(txtRgb,2);
    QHBoxLayout *hLayoutXY=new QHBoxLayout();
    hLayoutXY->addWidget(labXY,1);
    hLayoutXY->addWidget(txtXY,2);

    QVBoxLayout *vLayoutRight=new QVBoxLayout();
    vLayoutRight->addLayout(hLayoutWeb);
    vLayoutRight->addLayout(hLayoutRgb);
    vLayoutRight->addLayout(hLayoutXY);
    vLayoutRight->addWidget(labTip);

    QHBoxLayout *hLayout=new QHBoxLayout();
    hLayout->addLayout(hLayoutColor);
    hLayout->addLayout(vLayoutRight);

    hLayout->setStretchFactor(hLayoutColor,1);
    hLayout->setStretchFactor(vLayoutRight,1);

    this->setLayout(hLayout);

    this->installEventFilter(this);// 安装事件过滤器

}

void GetColorValueTools::ShowColorValue()
{
    QString text;
    if (isMouseButtonPress)
        text = "按下鼠标右键<span style=\"color:red;\">停止</span>/开启";
    else
        text = "按下鼠标右键停止/<span style=\"color:blue;\">开启</span>";
    labTip->setText(text);

    if(isMouseButtonPress)
        return;

    int x = QCursor::pos().x();
    int y = QCursor::pos().y();

    txtXY->setText(tr("X:%1  Y:%2").arg(x).arg(y));
    QString strDecimalValue,strHex;
    // 根据鼠标位置截取屏幕上的一个2x2像素区域的图像
    QScreen *screen = qApp->primaryScreen();  // 获取主屏幕的QScreen对象
    QPixmap pixmap = screen->grabWindow(qApp->desktop()->winId(), x, y, 2, 2);
    if (!pixmap.isNull())
    {
        QImage image = pixmap.toImage();
        if (!image.isNull())
        {
            if (image.valid(0, 0))  //验证图像中的像素 (0, 0) 是否有效,确保要获取(0, 0)的颜色值存在于图像中
            {
                QColor color = image.pixel(0, 0);//获取该图像中 (0,0) 像素的颜色
                int red=color.red();
                int green=color.green();
                int blue=color.blue();
                // 结果转换为一个两位的十六进制字符串，并且如果不足两位，则使用 '0' 来进行填充
                QString strRed=QString("%1").arg(red&0xFF,2,16,QLatin1Char('0'));
                QString strGreen=QString("%1").arg(green&0xFF,2,16,QLatin1Char('0'));
                QString strBlue=QString("%1").arg(blue&0xFF,2,16,QLatin1Char('0'));

                strDecimalValue = tr("%1, %2, %3").arg(red).arg(green).arg(blue);
                // toUpper()所有字符转换为大写形式
                strHex=tr("#%1%2%3").arg(strRed.toUpper()).arg(strGreen.toUpper()).arg(strBlue.toUpper());
            }
        }
    }

    QString str=tr("background-color: rgb(%1);").arg(strDecimalValue);
    labColor->setStyleSheet(str);
    txtRgb->setText(strDecimalValue);
    txtWeb->setText(strHex);
}


